package com.inspector.app.ui.splash

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import com.inspector.app.databinding.SplashActBinding
import com.inspector.app.ui.base.BaseDJActivity
import com.inspector.app.ui.login.LoginActivity
import com.inspector.app.ui.main.MainActivity
import com.inspector.app.util.CommenArgs
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseDJActivity() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, SplashActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

    private lateinit var binding: SplashActBinding
    private var mHadExitAnimator = false
    override fun onCreate(savedInstanceState: Bundle?) {
        binding = SplashActBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            splashScreen.setOnExitAnimationListener {
                it.remove()
                mHadExitAnimator = true
                startRouteActivity()
            }
            MainScope().launch {
                delay(200)
                if (mHadExitAnimator) {
                    return@launch
                }
                mHadExitAnimator = true
                startRouteActivity()
            }
        } else {
            startRouteActivity()
        }
//        val splashScreen = installSplashScreen()
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.splash_act)
//        splashScreen.setKeepOnScreenCondition { true }
//        startRouteActivity()
    }

    private fun startRouteActivity() {
        binding.root.postDelayed(Runnable {
            if (CommenArgs.getIsLogin()) {
                startActivity(Intent(this, MainActivity::class.java))
            } else {
                startActivity(Intent(this, LoginActivity::class.java))
            }
            finish()
        }, 3000)

    }
}